﻿namespace ImageProcessing.UI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// IMG_LinearOperations class
    /// </summary>
    public partial class IMG_LinearOperations : Form
    {
        /// <summary>
        /// original image bitmap
        /// </summary>
        private Entities.IMG_Image imgOriginal;

        /// <summary>
        /// edited image bitmap
        /// </summary>
        private Entities.IMG_Image imgEdited;

        /// <summary>
        /// parent Form referance
        /// </summary>
        private UI.IMG_HomeScreen parentForm;

        /// <summary>
        /// Initializes a new instance of the <see cref="IMG_LinearOperations"/> class.
        /// </summary>
        /// <param name="img_original">The img_original.</param>
        /// <param name="parent">The parent.</param>
        public IMG_LinearOperations(Entities.IMG_Image img_original, UI.IMG_HomeScreen parent)
        {
            try
            {
                this.InitializeComponent();
                this.parentForm = parent;
                if (img_original != null)
                {
                    this.imgOriginal = img_original;
                    this.pictureBoxOriginal.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(img_original);
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Loading Form");
            }
        }

        /// <summary>
        /// Handles the FormClosing event of the IMG_LinearOperations control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void IMG_LinearOperations_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.parentForm.ResetImage(this.imgEdited == null ? this.imgOriginal : this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, closing");
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnOpen control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.ppm)|*.jpg; *.jpeg; *.gif; *.bmp; *.ppm";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string img_OpenedFilePath = openFileDialog1.FileName;
                    this.imgOriginal = IMG_Processor.IMG_OpenSave.IMG_Open(img_OpenedFilePath);
                    this.pictureBoxOriginal.Image = this.imgOriginal.BitmapOfImage();
                    this.imgEdited = null;
                    this.pictureBoxEdited.Image = null;
                }
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error, Can't Open Image");
            }
        }

        /// <summary>
        /// Handles the Click event of the btn_MeanSmoothing control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_MeanSmoothing_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_MeanSmoothing((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);                
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Mean smoothing");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_GausianSmoothing control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_GausianSmoothing_Click(object sender, EventArgs e)
        {
            try
            {
                float img_maskSize = float.Parse(this.txtSigmaValue.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_GausianSmoothing((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (System.OutOfMemoryException img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Gaussian smoothing very large sigma");
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error Gaussian smoothing ");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_LineSharpening control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_LineSharpening_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_PointSharpening((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error linear sharpening");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_HorizontalSharpening control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_HorizontalSharpening_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_LineHorizontalSharpening((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error horizontal sharpening");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_VerticalalSharpening control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_VerticalalSharpening_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_LinevVerticalSharpening((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error vertical sharpening");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_diag2Sharpening control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_diag2Sharpening_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_LineDiagonal2Sharpening((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error diag 2 sharpening");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_diag1Sharpening control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_diag1Sharpening_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_LineDiagonal1Sharpening(this.imgOriginal, img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error diag 1 sharpening");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_PointEdgeDetection control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_PointEdgeDetection_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_PointEdgeDetection((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error poing detection");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_horizontalEdgeDetection control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_horizontalEdgeDetection_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_HorizontalEdgeDetection((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error horizontal edge detection");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_verticalEdgeDetection control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_verticalEdgeDetection_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_VerticalEdgeDetection((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error vertical edge detection");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_diag2EdgeDetection control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_diag2EdgeDetection_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_Diagonal2EdgeDetection((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error diag 2 edge detection");
            }
        }

        /// <summary>
        /// Handles the Click event of the Btn_diag1EdgeDetection control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_diag1EdgeDetection_Click(object sender, EventArgs e)
        {
            try
            {
                int img_maskSize = int.Parse(this.txtMaskSize.Text);
                if (img_maskSize == 0)
                {
                    throw new Exception("Mask must have a value");
                }

                this.imgEdited = ImageProcessor.IMG_NeighborhoodOperations.IMG_DiagonalEdgeDetection((this.imgEdited == null ? this.imgOriginal : this.imgEdited), img_maskSize);
                this.pictureBoxEdited.Image = IMG_Processor.IMG_OpenSave.IMG_BufferToBItmap(this.imgEdited);
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                MessageBox.Show("Error diag 1 edge detection");
            }
        }
    }
}
